﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class UserCenter_user_Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
   
    // 登陆
    protected void userlogin_Click(object sender, ImageClickEventArgs e)
    {
        BLL.Business.UserBLL userBLL = new BLL.Business.UserBLL();
        BLL.Business.ShoppingCarBLL carBLL = new BLL.Business.ShoppingCarBLL();

        String UserName = this.username.Text.ToString();
        String Password = this.password.Text.ToString();
        try
        {
            if (userBLL.LoginSucess(UserName, Password))
            {
                // -------------------  初始化购物车START  ----------------------
                Session["UserName"] = UserName;             // 记录用户名
                int UserId = Int32.Parse((userBLL.SelectByName(UserName)).Tables[0].Rows[0]["User_Idint"].ToString());

                DataSet ds = carBLL.SelectByUserId(UserId); // 根据查找购物车信息
                if ((ds == null) ||  // 如果购物车没有信息，不用管
                   (ds.Tables.Count == 0) ||
                   (ds.Tables.Count == 1 && ds.Tables[0].Rows.Count == 0))
                { }
                else                 // 如果含有信息，就把购物车中的物品添加到临时Session中
                {
                    Session["publicCar"] = new Hashtable();
                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {

                        String bookId = dr["Book_Idint"].ToString();
                        String userId = dr["User_Idint"].ToString();
                        String quantity = dr["Car_Quantityint"].ToString();

                        if (!((Hashtable)Session["publicCar"]).Contains(bookId))
                            ((Hashtable)Session["publicCar"]).Add(bookId, quantity);
                    }
                }
                // -------------------  初始化购物车END  ------------------------
                //Response.Write("<script language=javascript>history.go(-2);</script>");
                Response.Redirect("../Default.aspx");
            }
            else
                Response.Write("<script>alert('密码不正确或是用户名不存在！请重新输入')</script>");
        }
        catch (Exception ex)
        {
            Response.Write("<script>alert('" + ex.Message.ToString() + "')</script>");
        }
    }
    //protected void Unnamed1_Click(object sender, EventArgs e)
    //{
    //    form1.Target = "_black";
    //    Response.Write("<script>window.open('../GetPassword.aspx','_blank');</script>"); // IE可以再新窗口打开 chrome不行
    //    Response.Redirect("../GetPassword.aspx");

    //}
}
